﻿import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.2
import QtQuick.Controls.Styles 1.4


ApplicationWindow {
    visible: true
    width: Screen.width
    height: Screen.height
    title: qsTr("萌梦气球")

    property real screenWidth: Screen.width
    property real screenHeight: Screen.height

    property real ballRadius: 500

    property real ballCenterX: 0
    property real ballCenterY: 0

    property var colors: ["gray", "red", "yellow", "green"]

    property int score: 0

    property bool isGameOver: false

    property int maxScore: 0

    property int touchPower: 30

    property int gravity: 1

    property var items: new Array

    property real speed: 0  //y方向


    Component.onCompleted: {
//        anim.restart()
    }

    MouseArea{
        anchors.fill: imgBg
        onClicked: {
            if(!timerGame.running){
                timerGame.restart()
            }

            tip.visible = false

            if(imgBalloon.y >= 0){

                score++

                speed = speed * Math.sin(90)
                var ay = -touchPower * Math.sin(90)
                speed += ay

                imgBalloon.y += speed

                if(animTip.running){
                    animTip.complete()
                }
                animTip.restart()
            }
        }
    }

    Image{
        id: imgBg
        anchors.fill: parent
        source: "qrc:/img/img/bg.png"
        fillMode: Image.Stretch
    }

    Image{
        id: imgHot1
        x: 200
        y: parent.height/2
        source: "qrc:/img/img/hotball.png"
    }

    Image{
        id: imgHot2
        x: parent.width - width - 200
        y: parent.height/3*2
        source: "qrc:/img/img/hotball.png"
    }

    Text{
        id: tip
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 200
        color: "black"
        text: "点击屏幕"
        font.family: "微软雅黑"
        font.pointSize: 20
    }


    Image{
        id: imgBalloon
        x: (parent.width-width)/2
        y: (Screen.height-imgBalloon.height)/3*1
        source: "qrc:/img/img/balloon.png"
        z: 1

        Text{
            id: tipScore
            anchors.horizontalCenter: parent.horizontalCenter
            y: 50
            color: "white"
            text: score + "+"
            font.family: "微软雅黑"
            font.pointSize: 30
            opacity: 0
        }
    }


    Button{
        anchors.right: parent.right
        text: "提示"
        z: 1
        style: ButtonStyle{}
        onClicked: {
            messageDialog.visible = true
        }
    }

    MessageDialog{
        id: messageDialog
        visible: false
        title: "游戏提示"
        informativeText: "点击屏幕！"
    }



    MessageDialog{
        id: messageDialogRestart
        visible: false
        title: "萌梦气球"
        informativeText: "重新开始？"
        onAccepted: {
            score = 0
            isGameOver = false
            imgBalloon.y = (Screen.height-imgBalloon.height)/3*1
            timerGame.restart()
        }
    }

    //生命值
    Row{
        id: itemHead
        spacing: 100
        x: 10
        y: 10
        z: 1

        Text{
            id: scoreText
            text: "分数：" + String(score)
            font.pixelSize: 50
            font.family: "微软雅黑"
        }
    }


    Timer{
        id: timerGame
        interval: 30
        repeat: true
//        running: true
        onTriggered: {
            if(funcIsGameOver()){
                isGameOver = true
                timerGame.stop()

                maxScore = Math.max(maxScore, score)

                messageDialogRestart.visible = true
            }
            else{
                //模拟重力
                speed = speed * Math.sin(90)
                var ay = gravity * Math.sin(90)
                speed += ay
                imgBalloon.y += speed
            }
        }
    }

    SequentialAnimation{
        id: anim
        loops: Animation.Infinite
        running: true
        ParallelAnimation{
            PropertyAnimation{target: imgHot1; property: "y"; to: Screen.height/2-100; duration: 2000}
            PropertyAnimation{target: imgHot2; property: "y"; to: Screen.height/3*2+100; duration: 2000}
        }

        ParallelAnimation{
            PropertyAnimation{target: imgHot1; property: "y"; to: Screen.height/2; duration: 2000}
            PropertyAnimation{target: imgHot2; property: "y"; to: Screen.height/3*2; duration: 2000}
        }
    }

    ParallelAnimation{
        id: animTip
        PropertyAnimation{target: tipScore; property: "y"; from: 50; to: 30; duration: 500}
        PropertyAnimation{target: tipScore; property: "opacity"; from: 1; to: 0; duration: 500}
    }

    function funcIsGameOver(){
        //检测是否被球碰到，死亡

        if(imgBalloon.y >= Screen.height){
            return true
        }
        return false
    }

    function addZero(value){
        if(value < 10){
            return "0" + value
        }
        return value
    }

    function getSeed(){

        var date = new Date
        var y = date.getFullYear()
        var M = addZero(date.getMonth())
        var d = addZero(date.getDate())
        var h = addZero(date.getHours())
        var m = addZero(date.getMinutes())
        var s = addZero(date.getSeconds())

        var time = String(y) + String(M) + String(d) + String(h) + String(m) + String(s)

        return Number(time)
    }

}

